【基础】python |
您所在的位置:网站首页 › word删除section break › 【基础】python |
概述
python-docx可以对word进行一些设置,比如布局等等,全部设置的话过于繁琐,之前python docxtpl可以直接通过word设置,比较方便,不过若是有些不得不用代码设置的需求,可以考虑以下方法。 这里引入一个概念section,即为节,章节,章节的范围比段落paragraph要大。这部分的设置对应于word中的页面布局,如下图
可以设置页眉的纸张方向,页面边距,页面大小,每个节可以设置单独的页眉页脚,也可以设置其页面布局,比如横向纵向。 纸张方向 from docx import Document from docx.enum.section import WD_ORIENT doc = Document('xxx.docx') sections = doc.sections for section in sections: new_width, new_height = section.page_height, section.page_width section.orientation = WD_ORIENT.LANDSCAPE section.page_width = new_width #需要同时设置width,height才能成功 section.page_height = new_height doc.save('yyy.docx')页面边距 可以为每个section设置属性: 左右上下边距: [left|right|top|bottom]_margin 页眉/页脚边距: [header|footer]_distance |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |